import Vue       from 'vue';
import iView     from 'iview';
import VueRouter from 'vue-router';
import Vuex      from 'vuex';
import VueI18n   from 'vue-i18n';
import Echarts   from 'echarts';
import jQuery    from 'jquery';
import Sprintf   from 'sprintf-js';

import Routers   from '@/router.js';
import Util      from '@/libs/util.js';
import Stores    from '@/store.js';
import App       from '@/App.vue';
import 'iview/dist/styles/iview.css';
import '@/views/css/common.css';

import Locales  from '@/locale.js';
import zhLocale from 'iview/src/locale/lang/zh-CN';
import enLocale from 'iview/src/locale/lang/en-US';

Vue.use(VueRouter);
Vue.use(Vuex);
Vue.use(VueI18n);
Vue.use(iView);

// 自动设置语言
const navLang = navigator.language;
const localLang = (navLang === 'zh-CN' || navLang === 'en-US') ? navLang : false;
const lang = window.localStorage.getItem('language') || localLang || 'zh-CN';

Vue.config.lang = lang;

// 多语言配置
const locales = Locales;
const mergeZH = Object.assign(zhLocale, locales['zh-CN']);
const mergeEN = Object.assign(enLocale, locales['en-US']);
Vue.locale('zh-CN', mergeZH);
Vue.locale('en-US', mergeEN);

// 路由配置
const RouterConfig = {
    //mode: 'history',
    routes: Routers
};
const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start();
    if(to.name == 'default') {
        next({
            name: 'request'
        });
    }
    Util.title(to.meta.title);
    next();
});

router.afterEach(() => {
    iView.LoadingBar.finish();
    window.scrollTo(0, 0);
});


const store = new Vuex.Store(Stores);


new Vue({
    el: '#app',
    router: router,
    store: store,
    render: h => h(App)
});
